package com.raulraja.persistence.impl;

import com.raulraja.model.domain.PersistentObject;
import com.raulraja.persistence.PersistenceAdapter;
import org.springframework.orm.hibernate3.HibernateTemplate;

import java.io.Serializable;

/**
 * Spring's hibernate templates based impl for PersistenceAdapter
 */
public class HibernateTemplatePersistenceAdapterImpl<T extends PersistentObject> implements PersistenceAdapter<T> {

	private HibernateTemplate hibernateTemplate;

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	/**
	 * Finds a persistent object in the data store based on its id
	 * Unfortunatly the hibernate template does not support generics
	 *
	 * @param entityClass the persistent object class
	 * @param id		  the persistent object unique identifier
	 * @return the persistent object if found
	 */
	@SuppressWarnings("unchecked")
	public T findById(Class<T> entityClass, Serializable id) {
		return (T) hibernateTemplate.load(entityClass, id);
	}
}
